from copy import deepcopy
from app.product.sku.model import Sku as SkuTable
from app.product.sku.model import SkuToUnit as SkuToUnitTable

from app.product.sku import dao
from common.mixins import DictMixin
from common.utils.utils import custom_marshal_pagination, custom_marshal_list
from common.utils.pinyin import get_first_letter_pinyin
from exts import db
import itertools


# ------------- route ------------
# def _create(user_id, **kwargs):
#     if not kwargs.get('short_name') and kwargs.get("name"):
#         kwargs['short_name'] = get_first_letter_pinyin(kwargs.get("name"))
#     new_obj = DictMixin.from_dict(SkuTable, kwargs)
#     new_obj.creator_id = user_id
#     db.session.add(new_obj)
#     return new_obj


# def create(user_id, **kwargs):
#     new_obj = _create(user_id, **kwargs)
#     db.session.commit()
#     return new_obj

#
# def get_by_page(user_id, **kwargs):
#     pagination = dao.get_by_page(**kwargs)
#     pagination = custom_marshal_pagination(pagination)
#     return pagination


# def get(user_id, obj_id):
#     return SkuTable.query.filter_by(id=obj_id).first()
#
#
# def _update(user_id, obj_id, **kwargs):
#     obj = SkuTable.query.filter_by(id=obj_id).first()
#     old_obj = deepcopy(obj)
#     new_obj = DictMixin.from_dict(obj, kwargs)
#     new_obj.updater_id = user_id
#     # 当名称发生修改时，自动更新short_name
#     if new_obj.name and old_obj.name != new_obj.name:
#         new_obj.short_name = get_first_letter_pinyin(new_obj.name)
#     return new_obj
#
#
# def update(user_id, obj_id, **kwargs):
#     new_obj = _update(user_id, obj_id, **kwargs)
#     db.session.commit()
#     return new_obj
#
#
# def delete(user_id, obj_id):
#     obj = SkuTable.query.filter_by(id=obj_id).first()
#     db.session.delete(obj)
#     db.session.commit()
#     return obj.id
#
#
# def delete_many(user_id, obj_ids_list):
#     objs = SkuTable.query.filter(SkuTable.id.in_(obj_ids_list)).all()
#     [db.session.delete(u) for u in objs]
#     db.session.commit()
#     return obj_ids_list
#
#
# def batch_update(user_id, obj_ids_list, **kwargs):
#     for obj_id in obj_ids_list:
#         _update(user_id, obj_id, **kwargs)
#     db.session.commit()
#     return obj_ids_list


# def create_sku_by_spu(user_id, spu_id, spec_attr):
#     own_specs = calculate_own_specs(spec_attr)
#     new_skus = []
#     for own_spec in own_specs:
#         new_sku = SkuTable.create(own_spec=own_spec, spu_id=spu_id)
#         # new_sku = _create(user_id, own_spec=own_spec, spu_id=spu_id)
#         new_skus.append(new_sku)
#     return new_skus


# def _create_sku_to_unit(user_id, sku_id, unit_id, **kwargs):
#     new_obj = DictMixin.from_dict(SkuToUnitTable, {
#         "sku_id": sku_id,
#         "unit_id": unit_id,
#         **kwargs,
#     })
#     new_obj.creator_id = user_id
#     db.session.add(new_obj)
#     return new_obj


# def _update_sku_to_unit(user_id, sku_id, unit_id, **kwargs):
#     obj = SkuToUnitTable.query.filter_by(sku_id=sku_id, unit_id=unit_id).first()
#     new_obj = DictMixin.from_dict(obj, kwargs)
#     new_obj.updater_id = user_id
#     return new_obj


# def create_sku_to_unit_by_spu(user_id, skus, unit_ids):
#     for sku in skus:
#         for unit_id in unit_ids:
#             SkuToUnitTable.create(sku_id=sku.id,
#                                   unit_id=unit_id)
#             # _create_sku_to_unit(user_id, sku_id=sku.id, unit_id=unit_id)


# def calculate_own_specs(spec_attr_json):
#     """
#     spec_attr_json eg:
#     {
#         1: [1,2]
#         2: [3,4]
#     }
#
#     own_specs eg:
#     [{1: 1, 2: 3}, {1: 1, 2: 4}, {1: 2, 2: 3}, {1: 2, 2: 4}]
#     """
#     # 转换成数组
#     specs = []
#     for spec_key in spec_attr_json:
#         one_spec = []
#         for attr in spec_attr_json[spec_key]:
#             one_spec.append({spec_key: attr})
#         specs.append(one_spec)
#     own_specs = []
#     for x in itertools.product(*specs):
#         own_spec = {}
#         for ele in x:
#             own_spec.update(ele)
#         own_specs.append(own_spec)
#     return own_specs


# 获取spu skus
# def get_skus_by_spu_id(spu_id):
#     sku_tables = SkuTable.query.filter_by(spu_id=spu_id).all()
#     return sku_tables

# def batch_update_skus_by_spu_id(spu_id, skus_list_json):
#     for sku_json in skus_list_json:
#         sku_table = SkuTable.query.filter_by(id=sku_json['sku_id']).first()
#         if not sku_table:
#             # 缺少的创建
#             SkuTable.create(**sku_json)
#         else:
#             # 已存在的更新
#             sku_table.update(**sku_json)
#     # 多余的删除
#     sku_tables = SkuTable.query.filter_by(spu_id=spu_id).all()
#     left_sku_id_list = [sku_json['sku_id'] for sku_json in skus_list_json]
#     for sku_table in sku_tables:
#         if sku_table not in left_sku_id_list:
#             sku_table.hard_delete()
#     db.session.commit()


# def batch_update_sku_by_spu_id(user_id, spu_id, skus_list_json): # 将废弃
#     for sku_json in skus_list_json:
#         sku_table = SkuTable.query.filter_by(id=sku_json['sku_id']).first()
#         if not sku_table:
#             # 创建
#             new_sku_table = _create(user_id, **sku_json)
#             db.session.add(new_sku_table)
#         else:
#             # 更新
#             _update(user_id, sku_table.id, **sku_json)
#     # 删除
#     sku_tables = SkuTable.query.filter_by(spu_id=spu_id).all()
#     left_sku_id_list = [sku_json['sku_id'] for sku_json in skus_list_json]
#     for sku_table in sku_tables:
#         if sku_table not in left_sku_id_list:
#             db.session.delete(sku_table)


# 获取spu sku_to_unit
# def get_sku_to_unit_by_spu_id(spu_id):
#     sku_to_unit = dao.get_sku_to_unit_by_spu_id(spu_id=spu_id)
#     return custom_marshal_list(sku_to_unit)


# def batch_update_sku_to_unit_by_spu_id(spu_id, sku_to_unit_list_json):
#     # 全部删除
#     old_all_sku_to_unit_json = get_sku_to_unit_by_spu_id(spu_id)
#     for ele in old_all_sku_to_unit_json:
#         del_sku_to_unit = SkuToUnitTable.query.filter_by(
#             sku_id=ele['sku_id'], unit_id=ele['unit_id'])
#         del_sku_to_unit.hard_delete()
#         # db.session.delete(del_sku_to_unit)
#     # 重新创建
#     for sku_to_unit_json in sku_to_unit_list_json:
#         SkuToUnitTable.create(
#             **sku_to_unit_json
#         )
#     db.session.commit()
        # new_sku_to_unit_table = _create_sku_to_unit(
        #     user_id,
        #     **sku_to_unit_json
        # )
        # db.session.add(new_sku_to_unit_table)
